/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package conexion;

import java.sql.*;
/**
 *
 * @author afernandez
 */
public class OracleDBConn {
    /**
	 *Permite Cargar en Memoria los Drivers
        
	 */
        private Connection conn = getConnection();
	static {
                String driver = "oracle.jdbc.driver.OracleDriver";
		try {
			Class.forName(driver);

		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}

	/**
	 * Obtiene una conexi�n a la Base de Datos.
	 */
	public Connection getConnection() {
		Connection connection = null;
                String url = "jdbc:oracle:thin:@192.168.17.20:1521:baan";
                String username = "baan";
                String password = "baan";

		try {
			connection =
			DriverManager.getConnection(url,username,password);

		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		return connection;
	}
    public  boolean isValidConnection() throws Exception {
        if (conn == null) { return false; }
        if (conn.isClosed()) { return false; }
        return testConnection( "select 1 from dual");
    }
    public  boolean testConnection( String query) {
        ResultSet rs = null;
        Statement stmt = null;
        try {
            stmt = conn.createStatement();
            if(stmt == null){ return false; }
            rs = stmt.executeQuery(query);
            if(rs == null){ return false; }
            if (rs.next()) { return true; }
            return false;
        } catch (Exception e) {
            return false;
        } finally {
            try {
              rs.close();
              stmt.close();
            }catch(Exception e) {
                // ignore
            }
        }
    }

}
